package com._5704.website.util;

import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.stream.IntStream;

/**
 * 获得随机工具类
 */
@Component
public class GetRandomUtil {
    /**
     * 获得随机字符串
     * @param length 长度
     * @return 字符串
     */
    public static String getRandomString(int length){
        Random random = new Random();

        IntStream ints = random.ints(33, 126);

        StringBuilder stringBuffer = new StringBuilder();

        ints.limit(length).forEach(e-> stringBuffer.append((char) e));

        return String.valueOf(stringBuffer);
    }

    /**
     * 获得随机数字
     * @param length 长度
     * @return 字符串
     */
    public static String getRandomCode(int length){
        Random random = new Random();

        IntStream ints = random.ints(0, 9);

        StringBuilder stringBuffer = new StringBuilder();

        ints.limit(length).forEach(stringBuffer::append);

        return stringBuffer.toString();
    }
}
